package yxy.game.pm2.active.module;

import org.joda.time.DateTime;
import yxy.apple.util.redisson.RedisException;
import yxy.cherry.data.bean.actcultivate;
import yxy.game.pm2.active.module.actcontrol.activeControl;
import yxy.game.pm2.active.module.actdata.ActDataCenter;
import yxy.game.pm2.active.module.subordinateclass.DevelopGiftPackage;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.PlayerBean;
import yxy.game.pm2.bean.ServerBean;
import yxy.game.pm2.bean.area.AreaBean;
import yxy.game.pm2.bean.db.dao.MySQLCacheDAO;
import yxy.game.pm2.bean.module.AreaModule;
import yxy.game.pm2.bean.module.PlayerModule;

import java.util.ArrayList;
import java.util.Map;

public class ActiveModule_养成礼包 extends PlayerModule {

	//bean ---------------------------------------------------
	
	public static final int acttype = 15;
	
	public static final int type_star_5 = 5;	//5星养成
	public static final int type_star_6 = 6;	//6星养成

	private DateTime startTime; //礼包冷却时间
	
	private ArrayList<DevelopGiftPackage> dgifts = new ArrayList<DevelopGiftPackage>();
	
	public ArrayList<DevelopGiftPackage> getDgifts() {
		return dgifts;
	}
	public void setDgifts(ArrayList<DevelopGiftPackage> dgifts) {
		this.dgifts = dgifts;
	}

	public DateTime getStartTime() { return startTime; }
	public void setStartTime(DateTime startTime) { this.startTime = startTime; }
	//fnc -----------------------------------------------------
	
//	public static final int canget = 1;
//	public static final int hadget = 2;
//	public static final int cannotget = 3;
	
	/**
	 * @param pcode
	 */
	public static ActiveModule_养成礼包 create(String pcode) {
//		PlayerBean pb = PlayerBean.get(pcode);
//		int sid = pb.getServerId();
//		ServerBean sb = ServerBean.get(sid);
//		int areaid = sb.getAreaId();

		AreaBean ab = AreaModule.area(pcode);
		activeControl sai = activeControl.get(ab.getAreaId(), acttype);
		if (sai != null) {
			ActiveModule_养成礼包 am = new ActiveModule_养成礼包();
			am.setPlayerCode(pcode);
			am.setTimeline(sai.getTimeline());
			am.set();
			return am;
		}

		return null;
	}

	public void update() {
		PlayerBean pb = PlayerBean.get(getPlayerCode());
		ServerBean sb = ServerBean.get(pb.getServerId());
		activeControl sai = activeControl.get(String.valueOf(sb.getAreaId()), String.valueOf(acttype));
		if(sai == null) {
			return;
		}


		// 关闭活动
		if (!sai.running() && running()) {
			setTimeline(sai.getTimeline());
			set();
		}

		// 开启活动
		if (sai.running() && !running()) {
			setTimeline(sai.getTimeline());
			setDgifts(new ArrayList<DevelopGiftPackage>());
			set();
		}
		
		boolean save = false;
		ArrayList<DevelopGiftPackage> newdgifts = new ArrayList<DevelopGiftPackage>();
		for (DevelopGiftPackage dgift : getDgifts()) {
			DateTime end = dgift.getEnd();
			
			if(DateTime.now().isAfter(end)) {
				save = true;
				continue;
			}

			DateTime locktime = dgift.getLocktime();
			if(locktime != null) {
				DateTime unlocktime = locktime.plusMinutes(5);
				if(DateTime.now().isAfter(unlocktime)) {
					dgift.setLock(false);
					dgift.setLocktime(null);
				}
			}
			
			newdgifts.add(dgift);
		}
		
		if(save) {
			setDgifts(newdgifts);
			set();
		}

	}

	/**
	 * 创建几星养成礼包
	 * @param star
	 */
	public void createDevelopGiftPackage(int star) {
		if(star != 5 && star != 6) {
			return;
		}

		for(DevelopGiftPackage dgift : getDgifts()){
			if(dgift.getStar() == star) {
				return;
			}
		}
		
		Map<String, actcultivate> actmap = ActDataCenter.getAllData(actcultivate.class, getPlayerCode());
		for(String key : actmap.keySet()) {
			actcultivate act = actmap.get(key);
			if(act.getType() == star) {
				DevelopGiftPackage dgift = new DevelopGiftPackage();
				dgift.setId(act.getId());
				dgift.setGoodsid(act.getPurchasever());
				dgift.setStar(star);
				
				DateTime start = DateTime.now();
				DateTime end = start.plusHours(2);
				
				dgift.setStart(start);
				dgift.setEnd(end);
				
				ArrayList<Currency> curs = (ArrayList<Currency>) Currency.create(act.getRewardtype(), act.getRewardid(), act.getRewardnum());
				dgift.setCurs(curs);
				
				getDgifts().add(dgift);
			}
		}
		
		set();
		
		{
			new ActOpenHandle().updateActOpen(getPlayerCode(), acttype, OpenType.礼包新开启);
		}
	}
	
	/**
	 * 能否购买礼包
	 * @param goodsid
	 * @return
	 */
	public boolean canBuyGiftPackage(int goodsid) {
		for(DevelopGiftPackage dgift : getDgifts()) {
			if(dgift.getGoodsid() == goodsid) {
				if(!dgift.isLock()) {
					return true;
				}
			}
		}
		return false;
	}
	
	/**
	 * 锁单
	 * @param goodsid
	 */
	public void lockGiftPackage(int goodsid) {
		for(DevelopGiftPackage dgift : getDgifts()) {
			if(dgift.getGoodsid() == goodsid) {
				dgift.setLock(true);
				dgift.setLocktime(DateTime.now());
				set();
				return;
			}
		}
	}
	
	/**
	 * 解锁
	 * @param goodsid
	 */
	public void unlockGiftPackage(int goodsid) {
		unlockGiftPackageAndSetBuyTime(goodsid, null);
	}
	
	/**
	 * 解锁单 加 设置购买时间
	 * @param goodsid
	 * @param buytime
	 */
	public void unlockGiftPackageAndSetBuyTime(int goodsid, DateTime buytime) {
		for(DevelopGiftPackage dgift : getDgifts()) {
			if(dgift.getGoodsid() == goodsid) {
				dgift.setLock(false);
				dgift.setLocktime(null);
				dgift.setHadbuy(true);
		
				set();
				return;
			}
		}
	}
	
	// redis ---------------------------------------------------------

	static private String key(String playerCode) {
		return String.format("PLAYER:%s:ACTIVE:%S", playerCode, acttype);
	}

	public void set() {
		try {
			setv(key(getPlayerCode()), this);
			check();
		} catch (RedisException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 获取 ActiveModule_限时抢购
	 * @param playerCode
	 * @return
	 */
	static public ActiveModule_养成礼包 get(String playerCode) {
		try {
			ActiveModule_养成礼包 module = getv(key(playerCode));
			if (module == null) {
				module = MySQLCacheDAO.getV(key(playerCode), ActiveModule_养成礼包.class);
				if(module != null){module.set();}//同步到redis

				if (module == null) {
					module = create(playerCode);
				}

				if(module == null){return null;}
			}

			module.update();
			module.check();
			return module;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/** 查看是否有可领取奖励 控制红点 */
	public void check() {
		if (running() && getDgifts().size() >= 1) {
			new ActOpenHandle().updateActOpen(getPlayerCode(), acttype, OpenType.开启);
		} else {
			new ActOpenHandle().updateActOpen(getPlayerCode(), acttype, OpenType.关闭);
		}
	}

	//存储到数据库的方法
	public static void saveToDB(String playerCode) {
		try {
			String key = key(playerCode);
			if (touchBucket(key).isExists()) {
				ActiveModule_养成礼包 activeModule_养成礼包 = getv(key(playerCode));
				MySQLCacheDAO.setV(key, activeModule_养成礼包);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	//从redis移走数据的操作
	public static void removeAllFromRedis(String playerCode) {
		try {
			touchBucket(key(playerCode)).delete();
		} catch (RedisException e) {
			e.printStackTrace();
		}
	}
	// lock==========================================================================

	@Override
	public String lockKey() {
		return String.format("lock:%s", key(getPlayerCode()));
	}

}
